<template>
  <div class="basic_info">
    <div class="info_operation_btn">
      <div class="btn_left">
        <el-button @click="exportExcel()" :disabled="!buttonsPermissions.DaoChu"
          :class="!buttonsPermissions.DaoChu ? 'disabled_button' : ''">导出</el-button>
        <el-button :disabled="!buttonsPermissions.DaYin" :class="!buttonsPermissions.DaYin ? 'disabled_button' : ''"
          @click="handleShortcutKeyPrint">打印(P)</el-button>
      </div>
      <div class="btn_right">
        <el-input v-model="queryData.keywords" placeholder="请输入日志内容\操作模块\操作对象"></el-input>
        <el-button type="primary" @click="onQuery">搜索</el-button>
        <el-button @click="onRefresh">刷新</el-button>
        <i @click="onShowColumn()" class="set_up el-icon-s-tools"></i>
      </div>
    </div>
    <div class="info_content_div">
      <div class="div_table">
        <div class="div_screen">
          <div class="screen_left">
            <div>
              <p>操作用户</p>
              <el-select placeholder="" v-model="queryData.createBy" class="screen_select" ref="yonghu">
                <el-option v-for="item in czyhOptions" :key="item.id" :label="item.realname" :value="item.id">
                </el-option>
              </el-select>
            </div>
            <div>
              <p>操作对象</p>
              <el-input v-model="queryData.operationObject" class="screen_select"></el-input>
            </div>
            <div>
              <p>操作类型</p>
              <el-input v-model="queryData.operationType" class="screen_select"></el-input>
            </div>
          </div>
          <div class="screen_right">
            <el-select @change="changeDate" v-model="monthValue">
              <el-option v-for="item in monthOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-date-picker v-model="nowDate" v-if="isShowDate" type="daterange" range-separator="~"
              value-format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期" @change="onClickPicker">
            </el-date-picker>
            <el-button type="primary" @click="onQuery">查询</el-button>
          </div>
        </div>
        <div class="table_content">
          <el-table highlight-current-row :header-cell-style="{
            color: '#666666',
            textAlign: 'center',
            height: '30px',
            borderColor: '#dddddd',
          }" :data="tableData" height="100%" style="width: 100%" :row-style="rowClass">
            <template v-for="(item, index) in columnData">
              <el-table-column :show-overflow-tooltip="true" :key="index"
                :label="item.zidingyiName ? item.zidingyiName : item.morenName" v-if="item.zidingyiState"
                :prop="item.props" min-width="130px" :sortable="item.sortable" :width="item.cellWidth">
              </el-table-column>
            </template>
          </el-table>
        </div>
        <div class="table_pagination">
          <div class="pagination_div">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="queryData.pageNo" :page-sizes="[20, 50, 100, 200]" :page-size="queryData.pageSize"
              layout="jumper, prev, pager,next, sizes" :total="queryData.total">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <!-- 列配置弹框 -->
    <CConfiguration ref="setColumnRef" :columnDataArr="columnData" :defaultColumnDataArr="defaultColumnData"
      orderType="操作日志" @onSaveColumn="onSaveColumn"></CConfiguration>
    <!-- 打印 -->
    <dialog-print ref="print"></dialog-print>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import CConfiguration from '@/components/c_column/columnConfiguration1'
import { dateStrFromDate } from '@/utils/DateFormatter'
import { getAction, postAction, putAction, downPostFile } from '@/api/manage'
/**打印 */
import dialogPrint from '../components/dialogPrintRecord.vue'
// 导入调用处理权限数据方法
import {
  handlePermissions,
  handleOtherPerms,
  getSystemParamsByName,
  formatMoney,
  formatPrice,
  formatNumber,
  delcommafy,
} from '@/utils/index'
import { ACCESS_TOKEN, TENANT_ID, ACCOUNT_BOOK } from '@/store/mutation-types'
import Vue from 'vue'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
export default {
  name: 'operationLog',
  components: {
    CConfiguration,
    dialogPrint,
  },
  mixins: [JeecgListMixin],
  computed: {
    ...mapGetters(['name']),
    buttonsPermissions() {
      return handlePermissions('操作日志')
    },
    printPermissions() {
      return handleOtherPerms('单据可多次打印')
    },
  },
  created() {
    // 默认获取当月时间
    this.getTimeRange(this.$setTimeRange(5))

    this.defaultColumnData = this.columnData
    this.selectTitle()
    this.getStaffData()
  },
  watch: {
    $route: {
      immediate: true,
      handler() {
        if (this.$route.name == 'home-operationLog') {
          let aId = this.$route.params.accountBookId
          if (aId != this.accountBookId) {
            this.accountBookId = aId
            this.onQuery()
          }
        }
      },
    },
  },
  data() {
    return {
      // 操作用户
      czyhOptions: [],
      // 查询条件数据
      queryData: {
        //搜索框值
        keywords: '',
        pageSize: 20, //每页的数据条数
        pageNo: 1, //默认开始页面
        createBy: '', //操作用户
        startDate: '', //开始时间
        finishDate: '', //结束时间
        operationObject: '', //操作日志
        operationType: '', // 操作类型
        total: 0, // 总数
      },
      // 月份选择
      monthValue: '本月',
      monthOptions: [
        {
          value: '今日',
          label: '今日',
        },
        {
          value: '本周',
          label: '本周',
        },
        {
          value: '本月',
          label: '本月',
        },
        {
          value: '本年',
          label: '本年',
        },
      ],
      isShowDate: true, // 是否显示选择时间组件
      nowDate: [],
      // 表格数据
      tableData: [],
      defaultColumnData: [], //默認列配置數據
      // 列配置数据
      columnData: [
        {
          id: '1',
          morenName: '序号',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 0,
          zidingyiSort: 0,
          noModify: true,
          sortable: true,
          cellWidth: '70px',
          props: 'xh',
        },
        {
          id: '2',
          morenName: '操作时间',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 1,
          zidingyiSort: 1,
          noModify: false,
          sortable: true,
          cellWidth: '170px',
          props: 'operationTime',
        },
        {
          id: '3',
          morenName: '手机号',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 2,
          zidingyiSort: 2,
          noModify: false,
          sortable: true,
          cellWidth: '100px',
          props: 'telephone',
        },
        {
          id: '4',
          morenName: '用户名称',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 3,
          zidingyiSort: 3,
          noModify: false,
          sortable: true,
          cellWidth: '170px',
          props: 'createName',
        },
        {
          id: '5',
          morenName: '操作模块',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 4,
          zidingyiSort: 4,
          noModify: false,
          sortable: true,
          cellWidth: '170px',
          props: 'operationModule',
        },
        {
          id: '6',
          morenName: '操作对象',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 5,
          zidingyiSort: 5,
          noModify: false,
          sortable: true,
          cellWidth: '100px',
          props: 'operationObject',
        },
        {
          id: '7',
          morenName: '操作类型',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 6,
          zidingyiSort: 6,
          noModify: false,
          sortable: true,
          cellWidth: '100px',
          props: 'operationType',
        },
        {
          id: '8',
          morenName: '内容描述',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 7,
          zidingyiSort: 7,
          noModify: false,
          sortable: true,
          cellWidth: '270px',
          props: 'operationContent',
        },
        {
          id: '9',
          morenName: 'IP',
          zidingyiName: '',
          morenState: true,
          zidingyiState: true,
          morenSort: 8,
          zidingyiSort: 8,
          noModify: false,
          sortable: true,
          cellWidth: '170px',
          props: 'ipAddress',
        },
      ],
      showLiepeizhi: false, //是否显示列配置
      updateId: '', // 修改列配置时返回的id
      accountBookId: '',// 账套id
      columnKey: '操作日志',
      url: {
        exportXlsUrl: '/xghOperationLog/xghOperationLog/exportXls'
      }, // 导出地址
      printClick: false,
    }
  },
  methods: {
    // 打印
    handleShortcutKeyPrint() {
      this.printClick = true
      this.onQuery()
    },
    handlePrint() {
      if (!this.buttonsPermissions.DaYin) {
        this.$elMessage.warning('未拥有打印权限！')
      } else if (!this.tableData || this.tableData.length <= 0) {
        this.$elMessage.warning('没有要打印的记录！')
      } else {
        let printData = {
          riqi: this.queryData.startDate.split(' ')[0] + '~' + this.queryData.finishDate.split(' ')[0],
          yonghu: this.$refs.yonghu.selected.label,
          printExclusive: this.tableData,
        }
        // 可以多次打印设置
        if (this.printPermissions) {
          this.$refs.print.show(printData, this.columnData, this.columnKey)
        } else {
          if (this.$lcStorage.get('printLimitValue')) {
            // 获取打印次数
            this.getPrintNum(this.columnKey, (() => {
              this.$refs.print.show(printData, this.columnData, this.columnKey)
            }))
          } else {
            this.$refs.print.show(printData, this.columnData, this.columnKey)
          }
        }
      }
    },
    // 获取数据
    getData() {
      let data = { ...this.queryData }
      if (this.accountBookId) {
        data.abId = this.accountBookId
        data.accountBookId = this.accountBookId
        data.xghAccountBookId = this.accountBookId
      }
      getAction('/xghOperationLog/xghOperationLog/list', data).then((res) => {
        if (res.code === 200) {
          this.queryData.total = res.result.total
          let data = res.result.records || []
          data.forEach((item, index) => {
            item.xh = index + 1 // 表格显示的行号码，从1开始
          })
          this.tableData = data
        }
        if (this.printClick) {
          this.handlePrint()
        }
        this.printClick = false
      })
    },
    // 刷新
    onRefresh() {
      this.queryData = this.$options.data().queryData
      this.monthValue = '本月'
      this.getTimeRange(this.$setTimeRange(5))
      this.getData()
    },
    // 查询
    onQuery() {
      this.queryData.pageNo = 1
      this.getData()
    },
    // 判断当前选择时间段
    changeDate(val) {
      this.isShowDate = true
      switch (val) {
        case '全部':
          this.isShowDate = false
          break
        case '今日':
          this.getTimeRange(this.$setTimeRange(1))
          break
        case '昨天':
          this.getTimeRange(this.$setTimeRange(2))
          break
        case '本周':
          this.getTimeRange(this.$setTimeRange(3))
          break
        case '上周':
          this.getTimeRange(this.$setTimeRange(4))
          break
        case '本月':
          this.getTimeRange(this.$setTimeRange(5))
          break
        case '上月':
          this.getTimeRange(this.$setTimeRange(6))
          break
        case '本年':
          this.getTimeRange(this.$setTimeRange(7))
          break
        case '去年':
          this.getTimeRange(this.$setTimeRange(8))
          break
        default:
          break
      }
    },
    // 获取时间范围并设置
    getTimeRange(data) {
      this.nowDate = [data.now, data.end]
      this.queryData.startDate = data.now + ' 00:00:00'
      this.queryData.finishDate = data.end + ' 23:59:59'
    },
    // 获取当前选中时间
    onClickPicker(val) {
      this.queryData.startDate = val[0] + ' 00:00:00'
      this.queryData.finishDate = val[1] + ' 23:59:59'
    },
    // 修改表格行样式
    rowClass(row) {
      if (row.row.operationType === '作废') {
        return { color: '#1993fb' }
      } else if (row.row.operationType === '反审核') {
        return { color: '#fd6c21' }
      } else if (row.row.operationType === '红冲') {
        return { color: '#ff0000' }
      }
      return { color: '#666666' }
    },
    /**
     *  列配置方法
     */
    // 获取线上的列配置
    async selectTitle() {
      let obj = await this.$setColumn.selectColumn(this.columnKey)
      if (obj.code === 200) {
        this.updateId = obj.updateId
        this.columnData = obj.columnData
      } else if (obj.code === 500) {
        let id = await this.$setColumn.addColumn(this.columnKey, this.defaultColumnData, 'props')
        this.updateId = id
        this.columnData = this.defaultColumnData
      } else if (obj.code === 404) {
        this.columnData = this.defaultColumnData
      }
    },
    // 触发列配置弹框
    onShowColumn() {
      this.$refs.setColumnRef.dialogVisible = true
    },
    // 修改列配置后保存修改后的值
    async onSaveColumn(value) {
      this.columnData = value
      let res = await this.$setColumn.editColumn(this.columnKey, this.columnData, this.updateId)
      if (res.code == 200) {
        this.$elMessage.success(res.message)
      } else {
        this.$elMessage.error(res.message)
      }
    },

    // 获取每页几条数据
    handleSizeChange(val) {
      this.queryData.pageSize = val
      this.getData()
    },
    // 获取当前第几页
    handleCurrentChange(val) {
      this.queryData.pageNo = val
      this.getData()
    },
    // 经手人信息-下拉框初始化
    getStaffData() {
      getAction('/dept/xghAccountBookDept/getAllDeptUsers', {}).then((res) => {
        if (res.code === 200) {
          this.czyhOptions = res.result
          const data = { realname: '所有用户', id: '' }
          this.czyhOptions.unshift(data)
        }
      })
    },
    // 导出
    exportExcel() {
      const fileName = '操作日志'
      let params = {
        ...this.queryData,
        fileName
      }
      this.handleExportXlsByStaff(fileName, params)
    },
  },
}
</script>

<style lang="scss" scoped>
.basic_info {
  width: 100%;
  height: calc(100vh - 110px);
  background: #f0f2f5;
  box-sizing: border-box;
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
}

.info_operation_btn {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;

  ::v-deep .el-button {
    width: 80px;
    height: 30px;
    text-align: center;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    border-radius: 5px;
    border: none;
    margin-right: 6px;
    margin-left: 0;
  }

  .el-button--default {
    color: #666;
  }

  ::v-deep .el-select {
    width: 80px;
    height: 30px;
    box-sizing: border-box;
    margin-right: 6px;

    .el-input {
      .el-input__inner {
        height: 30px;
        border: none;
        font-size: 12px;
      }

      .el-input__suffix {
        height: 30px;
        display: flex;
        align-items: center;
      }
    }
  }

  .btn_left {
    width: 60%;
    display: flex;
    justify-content: flex-start;
    align-items: center;

    p {
      font-size: 12px;
      color: #666;
      margin: 0 5px 0 9px;
    }

    ::v-deep .el-checkbox {
      display: flex;
      align-items: center;
      margin-left: 8px;

      .el-checkbox__input {
        height: 15px;
      }

      .el-checkbox__label {
        font-size: 12px;
        padding-left: 5px;
        color: #666;
      }

      .is-checked {
        .el-checkbox__inner {
          background: #1a93fa;
          border-color: #1a93fa;
        }
      }
    }
  }

  .btn_right {
    width: 40%;
    display: flex;
    justify-content: flex-end;
    align-items: center;

    .el-input {
      width: 250px;
      height: 30px;
      margin-right: 6px;

      ::v-deep .el-input__inner {
        height: 30px;
        border: none;
        box-sizing: border-box;
        padding: 0 8px;
        background: #fff;
        font-size: 12px;
        color: #666;

        &::placeholder {
          font-size: 12px;
          color: #c1c1c1;
        }
      }
    }

    .el-button {
      width: 60px;
    }

    .set_up {
      font-size: 23px;
      color: rgb(153, 153, 153);
    }
  }
}

.disabled_button {
  background: #cbced3 !important;
  color: #666 !important;
}

.info_content_div {
  width: 100%;
  height: calc(100% - 50px);
  background: #fff;
  box-sizing: border-box;
  padding: 0 13px;
  display: flex;

  .div_table {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;

    .div_screen {
      width: 100%;
      height: 30px;
      display: flex;
      margin-top: 15px;
      justify-content: space-between;

      ::v-deep .el-select {
        width: 120px;
        height: 30px;
        box-sizing: border-box;
        border-radius: 4px;

        .el-input {
          .el-input__inner {
            height: 30px;
            border-color: #e3e3e3;
            font-size: 12px;
            padding-left: 8px;
          }

          .el-input__suffix {
            height: 30px;
            display: flex;
            align-items: center;
          }
        }
      }

      input {
        width: 120px;
        height: 30px;
        background: #ffffff;
        border: 1px solid #e3e3e3;
        opacity: 1;
        border-radius: 4px;
        font-size: 12px;
        color: #666;
        box-sizing: border-box;
        padding: 0 8px;

        &:focus {
          outline: none;
          border-color: #1890ff;
        }
      }

      .screen_left {
        // (50+5+120+15)*4=760px
        width: 14.250891rem;
        height: 100%;
        box-sizing: border-box;
        display: flex;
        justify-content: flex-start;
        flex-wrap: wrap;

        >div {
          width: 170px;
          height: 30px;
          box-sizing: border-box;
          display: flex;
          align-items: center;
          margin-right: 14px;

          >p {
            margin: 0;
            width: 50px;
            margin-right: 5px;
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 400;
            color: #666666;
          }

          .screen_select {
            flex: 1;
            display: flex;
            height: 30px;
            align-items: center;
            margin-right: 0;
            text-align: left;
            text-align-last: left;
          }

          ::v-deep .el-input {
            width: 100%;

            .el-input__inner {
              height: 30px;
              border-color: #e3e3e3;
              font-size: 12px;
              padding-left: 5px;
            }

            .el-input__suffix {
              height: 30px;
              display: flex;
              align-items: center;
            }
          }
        }

        .line_dialog {
          .dialog_input {
            flex: 1;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: flex-end;
            margin-right: 0;
            text-align: left;
            text-align-last: left;

            ::v-deep .el-select {
              i {
                display: none;
              }
            }

            i {
              position: absolute;
              margin-right: 9px;
              color: #bfbfbf;
              font-size: 14px;
              cursor: pointer;
            }
          }
        }
      }

      .screen_right {
        // (50+5+120+15)*4=760px
        width: calc(100% - 14.250891rem);
        height: 100%;
        box-sizing: border-box;
        display: flex;
        justify-content: flex-end;
        align-items: flex-start;
        box-sizing: border-box;

        ::v-deep .el-button {
          width: 60px;
          height: 30px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 12px;
          border-radius: 5px;
          border: none;
        }

        ::v-deep .el-select {
          width: 80px;
          margin-right: 6px;
        }

        ::v-deep .el-date-editor {
          width: 3.375211rem;
          height: 30px;
          background: #ffffff;
          border: 1px solid #d9d9d9;
          margin-right: 0.112507rem;
          padding: 3px 6px;

          .el-range__icon {
            position: absolute;
            right: 0;
            line-height: 30px;
          }

          .el-range-separator {
            line-height: 24px;
          }

          .el-range-input {
            font-size: 12px;
          }

          .el-range__close-icon {
            display: none;
          }
        }
      }
    }

    .table_content {
      height: calc(100% - 75px);
      margin-top: 15px;

      ::v-deep .el-table {
        border: 1px solid #e3e3e3;
        box-sizing: border-box;

        th,
        td {
          padding: 0;
          box-sizing: border-box;
          border-right: 1px solid #e3e3e3;

          &:first-child {
            .cell {
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }

          &:last-child {
            border-right: none;
          }
        }

        .el-table__header {
          tr {
            &:last-child {
              th {
                background: rgb(237, 237, 237) !important;
              }
            }
          }
        }

        .cell {
          height: 30px;
          line-height: 30px;
          font-size: 12px;
        }

        // colgroup {
        //   width: 100% !important;
        //   display: flex;
        //   align-items: center;
        // }
        // col {
        //   flex: 1;
        // }
        // tr {
        //   width: 100%;
        //   display: flex;
        // }
        // .el-table__header {
        //   th {
        //     &:first-child {
        //       // max-width: 50px;
        //       max-width: .843803rem;
        //     }
        //     &:nth-child(2) {
        //       max-width: 1.312582rem;
        //     }
        //   }
        // }
        // .el-table__body {
        //   height: 30px;
        //   .el-table__row {
        //     td {
        //       // flex: 1;
        //       width: 100%;
        //       &:first-child {
        //         // flex: 0;
        //         // width: 500px !important;
        //         max-width: 50px;
        //         // .cell {
        //         //   order: -99;
        //         // }
        //       }
        //       &:nth-child(2) {
        //         max-width: 1.312582rem;
        //         .cell {
        //           order: -99;
        //         }
        //       }
        //       // &:last-child {
        //       //   max-width: 80px;
        //       //   .cell {
        //       //     order: 99;
        //       //   }
        //       // }
        //     }
        //   }
        //   .current-row {
        //     background: #e3f6ff;
        //   }
        // }
        .el-table__body tr:hover>td {
          background: #e3f6ff;
        }

        .el-table__footer-wrapper {
          td {
            background: #f4f4f4;
            border: none;
          }
        }

        //滚动条的宽度
        ::-webkit-scrollbar {
          width: 9px;
          height: 9px;
          background-color: #e5e5e5;
        }

        //滚动条的滑块
        ::-webkit-scrollbar-thumb {
          background-color: #c9c9c9;
        }
      }
    }

    .table_pagination {
      width: 100%;
      height: 60px;
      box-sizing: border-box;
      padding: 8px 0;
      display: flex;

      .pagination_div {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: flex-end;

        ::v-deep .el-pagination {
          padding: 0;
          height: 30px;

          .el-pagination__sizes {
            margin: 0;

            .el-input {
              margin-right: 0;
            }
          }
        }
      }
    }
  }
}
</style>